package top.jiangqiang.qianyi.web.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.jiangqiang.qianyi.common.constants.GlobalConstants;
import top.jiangqiang.qianyi.common.properties.AuthProperties;

/**
 * @author Jiangqiang
 * @version 1.0
 * @date 2022/12/9 14:03
 */
@Configuration
@RequiredArgsConstructor
public class DocConfiguration {
    private final AuthProperties authProperties;

    @Bean
    public GroupedOpenApi commonApi() {
        return GroupedOpenApi.builder()
                .group("前后台公共接口")
                .packagesToScan("top.jiangqiang.qianyi.api.common.controller")
                .pathsToMatch(GlobalConstants.CommonRequestURLConstants.COMMON_ADMIN_CONTROLLER_PATTERN)
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("后台接口")
                .packagesToScan("top.jiangqiang.qianyi.api.admin.controller")
                .pathsToMatch(GlobalConstants.AdminRequestURLConstants.ADMIN_CONTROLLER_PATTERN)
                .addOperationCustomizer((operation, handlerMethod) -> {
                            operation.addSecurityItem(new SecurityRequirement().addList(authProperties.getTokenKey()));
                            if (authProperties.getMock()) {
                                operation.addSecurityItem(new SecurityRequirement().addList(GlobalConstants.SecurityConstants.USER_ID));
                                operation.addSecurityItem(new SecurityRequirement().addList(GlobalConstants.SecurityConstants.PLATFORM));
                            }
                            return operation;
                        }
                )
                .build();
    }

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("前台接口")
                .packagesToScan("top.jiangqiang.qianyi.api.website.controller")
                .pathsToMatch(GlobalConstants.WebsiteRequestURLConstants.WEBSITE_CONTROLLER_PATTERN)
                .build();
    }

    @Bean
    public OpenAPI qianYiOpenApi() {
        return new OpenAPI()
                .components(components())
                .info(new Info().title("千异接口文档")
                        .description("极简的单体项目脚手架")
                        .version("v1.0.0")
                        .license(new License().name("GPL-3.0").url("https://www.gnu.org/licenses/gpl-3.0.html")))
                .externalDocs(new ExternalDocumentation()
                        .description("千异")
                        .url("https://www.jiangqiang.top"));
    }

    private Components components() {
        //定义安全方案
        Components components = new Components()
                .addSecuritySchemes(authProperties.getTokenKey(), new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .in(SecurityScheme.In.HEADER)
                        .name(authProperties.getTokenKey()));
        if (authProperties.getMock()) {
            //定义安全方案
            components.addSecuritySchemes(GlobalConstants.SecurityConstants.USER_ID, new SecurityScheme()
                            .type(SecurityScheme.Type.APIKEY)
                            .in(SecurityScheme.In.HEADER)
                            .name(GlobalConstants.SecurityConstants.USER_ID))
                    .addSecuritySchemes(GlobalConstants.SecurityConstants.PLATFORM, new SecurityScheme()
                            .type(SecurityScheme.Type.APIKEY)
                            .in(SecurityScheme.In.HEADER)
                            .name(GlobalConstants.SecurityConstants.PLATFORM));

        }
        return components;
    }
}
